package com.wanmait.gohome.controller.web;

import com.wanmait.gohome.pojo.Userinfo;
import com.wanmait.gohome.service.UserinfoService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/web/upload")
public class UploadController {
    @Resource
    private UserinfoService userinfoService;

    /*
        跳到显示图片页面
     */
    @GetMapping("showPositive")
    public void showPositive(){}

    /**
     * ifram跳转到图片上传页面
     */
    @GetMapping("uploadPositive")
    public void uplodaPositive()
    {}

    /**
     * ifram上传图片
     * @param idCardPic
     * @param request
     * @return
     */
    @PostMapping("uploadPositive")
    public ModelAndView uplodaPositive(MultipartFile idCardPic,HttpServletRequest request)
    {
        ModelAndView mv=new ModelAndView();
        String fileName="";
        try {
            String ext = FilenameUtils.getExtension(idCardPic.getOriginalFilename());//获取扩展名;
            fileName = UUID.randomUUID()+"."+ext;//这个是随机数加上扩展名;
            String realPath = request.getServletContext().getRealPath("/web/userinfo/img/uploadImages"+"/"+fileName);//获得图片路径加上图片名字;
            File file=new File(realPath);//保存图片的路径;
            idCardPic.transferTo(file);//上传图片到指定路径;
            mv.addObject("idCardPic",fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mv.setViewName("redirect:/web/upload/showPositive?idCardPic="+fileName);
        return  mv;
    }
    /**
     * ifram显示身份证背面上传界面
     */
    @GetMapping("uploadContrary")
    public void uploadContrary()
    {}
    @PostMapping("uploadContrary")
    public ModelAndView uploadContrary(MultipartFile idCardVerso,HttpServletRequest request)
    {
        ModelAndView mv= new ModelAndView();
        String fileName="";
        try {
            String ext=FilenameUtils.getExtension(idCardVerso.getOriginalFilename());//获取图片的扩展名;
            fileName=UUID.randomUUID()+"."+ext;//获得一个随机数加上图片的扩展名;
            String realPath = request.getServletContext().getRealPath("/web/userinfo/img/uploadImages"+"/"+fileName);//获得图片路径加上图片名字;
            File file=new File(realPath);//图片需要存放的路径;
            idCardVerso.transferTo(file);//transferTo方法就是把图片传到file这个路径里面;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mv.setViewName("redirect:/web/upload/showContrary?idCardVerso="+fileName);
        return mv;
    }
    /**
     * 显示背面身份证图片
     */
    @GetMapping("showContrary")
    public void showContrary()
    {

    }





}